package com.jiutong.meipengdai.ui.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiutong.meipengdai.R;
import com.jiutong.meipengdai.base.BaseActivity;
import com.jiutong.meipengdai.common.Constants;
import com.jiutong.meipengdai.entity.BaseRespond;
import com.jiutong.meipengdai.entity.IdentifyCodeRespond;
import com.jiutong.meipengdai.http.model.interfaces.RegisterAndLoginBiz;
import com.jiutong.meipengdai.http.present.UserInfoPresent;
import com.jiutong.meipengdai.utils.SpConfig;
import com.jiutong.meipengdai.views.dialog.IdCardTypeDialog;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作用:
 * Created by 78 on 2017/6/19.
 */
public class RealNameActivity extends BaseActivity implements RegisterAndLoginBiz {

    @Bind(R.id.tv_title)
    TextView tv_title;
    @Bind(R.id.rl_back)
    RelativeLayout rl_back;
    @Bind(R.id.et_real_name)
    EditText et_real_name;
    @Bind(R.id.et_id_num)
    EditText et_id_num;
    @Bind(R.id.et_vip_card)
    EditText et_vip_card;
    @Bind(R.id.btn_real_name_submit)
    Button btn_real_name_submit;
    @Bind(R.id.et_invite_code)
    EditText etInviteCode;
    @Bind(R.id.tv_idcard_type)
    TextView tvIdcardType;

    private UserInfoPresent userInfoPresent;
    private IdCardTypeDialog idCardTypeDialog;
    private String cardType;

    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_real_name;
    }

    @Override
    protected void initView() {
        super.initView();
        ButterKnife.bind(this);

        tv_title.setText("实名认证");

        userInfoPresent = new UserInfoPresent(this, this);
    }

    @OnClick({R.id.rl_back, R.id.btn_real_name_submit, R.id.tv_idcard_type})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.btn_real_name_submit:
                if (isRightAction()){
                    if("大陆身份证".equals(tvIdcardType.getText().toString())){
                        cardType = "1";
                    }else if("港澳台身份证".equals(tvIdcardType.getText().toString())){
                        cardType = "3";

                    }
                    userInfoPresent.authIdCard(getInviteCode(), cardType);
                }
                break;

            case R.id.tv_idcard_type:

                showIdCardTypeDialog();
                break;
        }
    }

    private void showIdCardTypeDialog() {

        if(idCardTypeDialog == null){
            idCardTypeDialog = new IdCardTypeDialog();

            idCardTypeDialog.setOnDialogClickListener(new IdCardTypeDialog.OnDialogClickListener() {
                @Override
                public void onBtnClick(String idType) {
                    tvIdcardType.setText(idType);
                }
            });
        }

        idCardTypeDialog.show(getSupportFragmentManager(), TAG);

    }

    //判断输入是否正确
    private boolean isRightAction() {
        if (TextUtils.isEmpty(getRelName())) {
            toastShortShow("请输入姓名");
            return false;
        }
        if (TextUtils.isEmpty(getIdCard())) {
            toastShortShow("请输入身份证号");
            return false;
        }
//        if (getIdCard().length() != 18) {
//            toastShortShow("您输入的身份证格式不对");
//            return false;
//        }

        if (!TextUtils.isEmpty(getCardNumber())) {
            if (getCardNumber().length() != 12) {
                toastShortShow("请输入12位的会员卡卡号");
                return false;
            }
        }

        if(TextUtils.isEmpty(tvIdcardType.getText().toString())){
            toastShortShow("请选择身份证类型");
            return false;
        }
        return true;
    }

    public String getInviteCode(){
        return etInviteCode.getText().toString().trim();
    }

    @Override
    public String getPhoneNum() {
        return null;
    }

    @Override
    public String getPassword() {
        return null;
    }

    @Override
    public String getVerificatCode() {
        return null;
    }

    @Override
    public String getOriPwd() {
        return null;
    }

    @Override
    public String getNewPwd() {
        return null;
    }

    @Override
    public String getRelName() {
        return et_real_name.getText().toString();
    }

    @Override
    public String getIdCard() {
        return et_id_num.getText().toString();
    }

    @Override
    public String getEmail() {
        return null;
    }

    @Override
    public String getNewPhone() {
        return null;
    }

    @Override
    public String getCardNumber() {
        return et_vip_card.getText().toString();
    }

    @Override
    public void onVerCodeGetSuccess(IdentifyCodeRespond subjects) {

    }

    @Override
    public void onSuccess(Object object) {
        BaseRespond respond = (BaseRespond) object;

        if (respond.getResCode().equals("200")) {
            toastShortShow(respond.getResDesc());
            Intent intent = new Intent();
            intent.putExtra("realName", getRelName());
            intent.putExtra("cardNumber", getCardNumber());
            SpConfig.getInstance().putBool(Constants.ISAUTH_BOOLEAN, true);
            SpConfig.getInstance().putString(Constants.REALNAME_STRING, getRelName());
            setResult(RESULT_OK, intent);
            finish();
        } else {
            toastShortShow(respond.getResDesc());
        }
    }

    @Override
    public void onError(Throwable e) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
